﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Quartz.NET.Web.Attr;
using Quartz.NET.Web.Extensions;
using Quartz.NET.Web.Models;
using Quartz.NET.Web.Utility;
using Quartz.Spi;
using System.Threading.Tasks;

namespace Quartz.NET.Web.Controllers
{

    public class TaskBackGroundController : Controller
    {
        private readonly ISchedulerFactory _schedulerFactory;
        private readonly IJobFactory _jobFactory;
        private readonly IFreeSql freeSql;
        public TaskBackGroundController(ISchedulerFactory schedulerFactory, IJobFactory jobFactory,IFreeSql _fsql)
        {
            this._jobFactory = jobFactory;
            this._schedulerFactory = schedulerFactory;
            freeSql=_fsql;
        }

        public IActionResult Index()
        {
            return View("~/Views/TaskBackGround/Index.cshtml");
        }

        /// <summary>
        /// 获取所有的作业
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetJobs()
        {
            return Json(await _schedulerFactory.GetJobs());
        }
        /// <summary>
        /// 获取作业运行日志
        /// </summary>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public IActionResult GetRunLog([FromBody]JObject PageInfo)
        {
            string taskId=PageInfo["taskId"].ToString();
            int page =PageInfo["page"]!=null? (int)PageInfo["page"]: 1;

            return Json(FileQuartz.GetJobRunLog(taskId, page));
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [TaskAuthor]
        public async Task<IActionResult> Add([FromBody]TaskOptions taskOptions)
        {
            return Json(await taskOptions.AddJob(_schedulerFactory,jobFactory:_jobFactory));
        }

        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
       [TaskAuthor]
        [HttpPost]
        public async Task<IActionResult> AddJob([FromBody]TaskOptions taskOptions)
        {
            return Json(await taskOptions.AddJob(_schedulerFactory,jobFactory:_jobFactory));
            
        }

        /// <summary>
        /// 切换任务状态
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [TaskAuthor]
        public async Task<IActionResult> SwitchJob([FromBody]TaskOptions taskOptions)
        {
            if(taskOptions.status==0)
            {
               return Json(await _schedulerFactory.Start(taskOptions));
            }
            else
            {
                 return Json(await _schedulerFactory.Pause(taskOptions));
            }
          
        }

        [TaskAuthor]
        [HttpPost]
        public async Task<IActionResult> UpdateJob([FromBody]TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Update(taskOptions));
        }

       /// <summary>
       /// 
       /// </summary>
       /// <param name="taskOptions"></param>
       /// <returns></returns>
        [TaskAuthor]
        public async Task<IActionResult> Remove([FromBody]TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Remove(taskOptions));
        }
        [TaskAuthor]
        public async Task<IActionResult> Update([FromBody]TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Update(taskOptions));
        }
        [TaskAuthor]
        public async Task<IActionResult> Pause([FromBody]TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Pause(taskOptions));
        }
        [TaskAuthor]
        public async Task<IActionResult> Start([FromBody]TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Start(taskOptions));
        }
        [TaskAuthor]
        public async Task<IActionResult> Run([FromBody]TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Run(taskOptions));
        }
    }




}